# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  layout 'flawmill'
  helper :all # include all helpers, all the time
  include SavageBeast::AuthenticationSystem

  before_filter :get_user

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => '7badeeca03b548630c01c04fc643d851'
  
  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  # filter_parameter_logging :password

  # do e-mail notifications
  def send_new_email( project, issue )
    project.members.each do |m|
      # next if s.doesnt_like_emails?
      next if m.email.nil? or m.email.empty?
      next unless m.notify_on_new?
      Notifier.deliver_new_issue( m, issue )
    end
  end

  def send_update_success_email( issue, comment )
    # make sure we contact each user only once!
    # dup so we don't affect the DB
    subscribers = issue.subscribers.dup
    issue.owners.each do |o|
      next if o.email.nil? or o.email.empty?
      if o.notify_on_update_if_owned?
        subscribers.delete o
        Notifier.deliver_updated_issue( o, issue, comment )
      end
    end

    subscribers.each do |s|
      next if s.email.nil? or s.email.empty?
      next unless s.notify_on_update_if_subscribed?
      Notifier.deliver_updated_issue( s, issue, comment )
    end
  end

  # Savage beast says that the current_user method needs to be implemented
  # in the application controller:
  #
  # http://www.williambharding.com/blog/rails/savage-beast-20/ , step # 7
  #
  # But some/all actions were unable to find the current_user method
  # (specifically topics/new), so I added a quick helper.
  #
  # FIXME: this method is duplicated in application_controller.rb and
  # application_helper.rb
  def current_user
    get_user
  end

  # FIXME: this method is duplicated in application_controller.rb and
  # application_helper.rb
  # FIXME: Add logic to this method
  def login_required
    false
  end
  
  # FIXME: this method is duplicated in application_controller.rb and
  # application_helper.rb
  # FIXME: Add logic to this method
  def authorized?
    @current_user
  end

  # FIXME: this method is duplicated in application_controller.rb and
  # application_helper.rb
  # FIXME: Add logic to this method
  def admin?
    @current_user and @current_user.admin
  end

private
  def authorize
    # relpace this with DN logic later
    unless User.find_by_id(session[:user_id])
      flash[:notice] = "Please log in"
      redirect_to(:controller => "welcome", :action => "login")
    else
      @current_user = User.find(session[:user_id])
    end
  end
  
  def authorize_admin
    # replace this with DN logic later
    unless session[:user_id] and User.find(session[:user_id]).admin
      flash[:notice] = "You are not logged in as an administrator"
      begin
        redirect_to :back
        rescue ::ActionController::RedirectBackError
          redirect_to :controller => "welcome", :action => "login"
      end
    else
      @current_user = User.find(session[:user_id])
    end
  end

  def get_user
    if not @current_user and session[:user_id]
      @current_user = User.find(session[:user_id])
    end
    @current_user
    # @current_user # = User.find_or_create_by_dn( 'cfuser' ) if RAILS_ENV == 'development'
  end

  def get_attachments
    Attachment # make rails load the attachment model, or else NameErrors
    attachments = []

    ##### existing attachments
    keys = params.keys.grep( /^existing_attachment_\d+$/ )
    keys.each do |a|
      attachments << Attachment.find( params[a][:id].to_i )
    end

    ##### new attachments
    keys = params.keys.grep( /^attachment_\d+$/ )
    keys.each do |a|
      type = params[a][:type]
      next if type.nil? or type.empty?

      # translate (project, local_id) => global id
      if type == 'IssueRef'
        # find the issue's global id
        i = Issue.find_project_local( params[a][:project],
                                      params[a][:local_id] )
        params[a].delete :project # else we get NoMethodError for #project=
        params[a].delete :local_id # `- same reason
        params[a][:ref_id] = i.id if i
      end

      attachment = type.constantize.new( params[a] )
      attachment.user = @current_user
      attachments << attachment
    end

    attachments.each do |a|
      a.save
    end

    # get rid of any nils
    attachments.compact
  end

end
